package site.wuao.simplerxjava.ui.activity;


import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

import site.wuao.simplerxjava.R;
import site.wuao.simplerxjava.bean.Item;
import site.wuao.simplerxjava.operator.ConditionOperator;
import site.wuao.simplerxjava.ui.adapter.RUAdapter;
import site.wuao.simplerxjava.ui.adapter.RUViewHolder;

/**
 * 首页
 *
 * @author wuao
 * @date 2017/7/9
 * @github {https://github.com/CoderWuao}
 * @blogs {http://wuao.site/}
 * @note -
 * ---------------------------------------------------------------------------------------------------------------------
 * @modified -
 * @date -
 * @github -
 * @blogs -
 * @note -
 */
public class MainActivity extends AppCompatActivity {
    /** 列表 */
    private RecyclerView mRvMain;
    /** 适配器 */
    private RUAdapter<Item> mRuMainAdapter;
    /** 数据 */
    private List<Item> mData = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initData();

        initView();
    }

    /**
     * 初始化布局
     */
    private void initView() {
        mRvMain = (RecyclerView) findViewById(R.id.rv_main);
        mRvMain.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        mRuMainAdapter = new RUAdapter<Item>(this, mData, R.layout.item_main) {
            @Override
            protected void onInflateData(RUViewHolder holder, Item data, int position) {
                holder.setText(R.id.tv_content, data.simpleClassName + " - " + data.methodName);
            }
        };
        mRuMainAdapter.setOnItemClickListener(new RUAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int itemType, int position) {
                try {
                    Item item = mRuMainAdapter.getData().get(position);
                    Class<?> clazz = Class.forName(item.className);
                    clazz.getMethod(item.methodName).invoke(clazz.newInstance());
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
        mRvMain.setAdapter(mRuMainAdapter);
    }

    /**
     * 初始化数据
     */
    private void initData() {
        mData = new ArrayList<>();

        List<Class> classes = new ArrayList<>();
//        classes.add(RxSubscribe.class);
//        classes.add(CreateOperator.class);
//        classes.add(ChangeOperator.class);
//        classes.add(FilterOperator.class);
//        classes.add(GroupOperator.class);
//        classes.add(HelpOperator.class);
//        classes.add(ErrorOperator.class);
//        classes.add(RxThread.class);
//        classes.add(BooleanOperator.class);
        classes.add(ConditionOperator.class);

        for (Class clazz : classes) {
            // 类中所有public的方法名字
            for (Method method : clazz.getMethods()) {
                String methodName = method.getName();
                switch (methodName) {
                    case "equals":
                    case "getClass":
                    case "hashCode":
                    case "notify":
                    case "notifyAll":
                    case "toString":
                    case "wait":
                        continue;
                    default:
                        break;
                }
                Item item = new Item();
                item.className = clazz.getName();
                item.simpleClassName = clazz.getSimpleName();
                item.methodName = methodName;
                mData.add(item);
            }
        }
    }
}
